#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   mobile_goods_code_mysql_togp.py    
@Contact :   pengwei.sun@aihuishou.com
@License :   (C)Copyright aihuishou

@Modify Time      @Author       @Version    @Desciption
------------      -----------   --------    -----------
2019-10-25 10:23   pengwei.sun      1.0         None
'''
from src.utils.config import logger
from src.utils.db_processor import mysql_prediction_processor,mysql_prediction_processorfat,presto_processor
from src.utils.util import get_today



def genertor_predict_batch( totalCnt, batch_size):
    '''
    参数：
        totalCnt: 总数
        batch_size:批次
    返回:
        一个generator，x: 获取的批次图片 y: 获取的图片对应的标签
    '''
    # while 1:
    for i in range(0, totalCnt + batch_size, batch_size):
        # x = i:i + batch_size]
        # 最重要的就是这个yield，它代表返回，返回以后循环还是会继续，然后再返回。就比如有一个机器一直在作累加运算，但是会把每次累加中间结果告诉你一样，直到把所有数加完
        yield i

def load_predict_save_data():

    totalCnt = 2539656
    batch_size = 100000
    logger.info('totalCnt:{};batchSize:{}'.format(totalCnt, batch_size))
    gen = genertor_predict_batch(totalCnt, batch_size)
    iter = 0
    while True:

        offset = next(gen)
        if (offset > totalCnt):
            break
        iter += 1
        logger.info('iter={};offset={} load_predict_save...'.format(iter, offset))
        data = load_mysql_data(batch_size, offset)

        save_model_data(data)
        logger.info('insert data iter={}'.format(iter))
def load_mysql_data(limit, offset):

    GOODS_CODE_COMPLETE_BATCH_DATA_SQL = """
    select *
    from 
    price_prediction.price_prediction_goods_code_06_01td limit {} offset {}
    """
    return mysql_prediction_processor.load_sql(GOODS_CODE_COMPLETE_BATCH_DATA_SQL.format(limit,offset))

def save_model_data(model_data):
    """
    保存补全数据
    :return:
    """
    logger.info(' save_model_data data...')
    insert_sql = """
      INSERT INTO ods.ods_warehouse_price_prediction_goods_code(id,goods_code,date,product_sku_key, product_sku_name, product_level_key, 
      product_level_name,product_key,product_name, product_category_id, product_brand_name, predict_origin, forecast_reference_price,
      is_new_product,POLY_pred_price
        )
      VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,  %s,%s, %s, %s)
      """

    INSERT_DATA_COLUMNS=['id','goods_code','date','product_sku_key',
        'product_sku_name', 'product_level_key',
        'product_level_name',
        'product_key',
        'product_name',
        'product_category_id',
        # product_category_name,
        'product_brand_name', 'predict_origin',
         'forecast_reference_price','is_new_product','POLY_pred_price']

    insert_data = model_data[INSERT_DATA_COLUMNS]
    # postgre_processor.insert_data_frame_to_pg(insert_data, 'ods.ods_warehouse_price_prediction_goods_code',
    #                                           feature_columns=INSERT_DATA_COLUMNS)
    presto_processor.execute_insert_sql(insert_sql,
                                         insert_data.to_records(index=False).tolist())
    return

def save_fat_mysql(model_data):
    insert_sql = """
                INSERT INTO price_prediction_c_sku_mobile_test_v1_new_not_virtual_weight(date, product_sku_key, product_sku_name, product_level_key, 
                product_level_name, product_key, product_name, product_category_id, product_category_name, product_brand_id,
                product_brand_name, predict_origin, forecast_reference_price,is_new_product,POLY_pred_price,final_price)
                VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,  %s,%s, %s, %s, %s)
                """
    mysql_prediction_processorfat.execute_insert_sql(insert_sql,
                                         model_data[
                                             ['date', 'product_sku_key', 'product_sku_name', 'product_level_key',
                                              'product_level_name', 'product_key', 'product_name',
                                              'product_category_id', 'product_category_name', 'product_brand_id',
                                              'product_brand_name', 'predict_origin', 'forecast_reference_price',
                                              'is_new_product',
                                              'POLY_pred_price','final_price']
                                         ].to_records(index=False).tolist())

if __name__ == '__main__':
    load_predict_save_data()